package getOffer.offer56_1;

/**
 * @Author: wjy
 * @Date: 2021/11/15 14:59
 */
public class Solution {

    public static void main(String[] args) {

        int[] ints = {4, 2, 4, 1};

        Solution solution = new Solution();
        int[] ints1 = solution.singleNumbers(ints);

        for (int i : ints1) {
            System.out.println(i);
        }

    }

    public int[] singleNumbers(int[] nums) {
        int x = 0, y = 0, n = 0, m = 1;
        // 1. 遍历异或
        for (int num : nums) {
            n ^= num;
        }
        // 2. 循环左移，计算 m !!!!!!!!!!!!!!!!!重要：m可以将两个不重复数字区分为上述两个子数组!!!!!!!!!!!!!!!!!!!!!!
        while ((n & m) == 0) {
            m <<= 1;
        }
        // 3. 遍历 nums 分组
        for (int num : nums) {
            // 4. 当 num & m != 0
            if ((num & m) != 0) {
                x ^= num;
            } else {
                // 4. 当 num & m == 0
                y ^= num;
            }
        }
        // 5. 返回出现一次的数字
        return new int[]{x, y};
    }

}
